#! /usr/bin/bash

cmakePath=$1
gccPath=$2
cxxPath=$3
softwareDir=$4
isCleanRebuild=$5

# 清除安装的可执行程序
# param: 要删除的targets
cleanInstallTargets(){
    echo "clean install targets in RK3308_ARM64 ..."
    for targetSite in ${*}
    do
        targetPath=${softwareDir}/build/edge_midware/${targetSite}
        if [ -e ${targetPath} ]
        then
            rm ${targetPath}
            echo " ${targetPath} has been cleaned ... "
        fi
    done
}


# 清除构建目标
# param：buildDir
cleanBuildDirectory(){
    #清除构建目标
    ${cmakePath} --build $1 --target clean -j 14 --
    #清除构建文件夹
    if [ -e $1 ]
    then
        rm -rf $1
    fi
}


#构建目标
#sourceDir=$1 buildDir=$2 buildTarget=$3
cmakeBuildTarget(){
    sourceDir=$1
    buildDir=$2
    buildTarget=$3

    if [ ${isCleanRebuild} == clean ]
    then
        cleanBuildDirectory ${buildDir}
        cleanInstallTargets master_coordinate_site
    fi

    if [ -e ${buildDir} ]
    then
        mkdir -p ${buildDir}
    fi

    #加载cmake
    ${cmakePath} -DRK3308_ARM64=true -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=${gccPath} -DCMAKE_CXX_COMPILER=${cxxPath} -S ${sourceDir} -B ${buildDir}
    #切换目录
    cd ${buildDir}
    #构建程序
    ${cmakePath} --build ${buildDir} --target ${buildTarget} -- -j 14

    #清除编译目录
    if [ ${isCleanRebuild} == clean ]
    then
        rm -rf  ${softwareDir}/query_config_ble_synergy_sites/cmakeBuild
    fi
}



#构建程序
cmakeBuildTarget ${softwareDir}/query_config_ble_synergy_sites ${softwareDir}/query_config_ble_synergy_sites/cmakeBuild/RK3308_ARM64 install 



